/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.entities.Services;
import model.entities.Discount;

/**
 *
 * @author Hieu.Nguyen
 */
public class DiscountDAO {
    private static DiscountDAO  discountDAO ;

    public static DiscountDAO  getInstance() {
        if (discountDAO  == null) {
            discountDAO  = new DiscountDAO ();
        }
        return discountDAO ;
    }
    
     public List<Discount> getDiscount(boolean isActived) {
        List<Discount> listDiscount = new ArrayList<Discount>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblDiscount";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Discount r = new Discount();
                r.setDiscountID(rs.getInt("DiscountID"));
                r.setDiscountName(rs.getString("DiscountName"));
                r.setDiscountPercent(rs.getFloat("DiscountPercent"));
                r.setDiscountAmount(rs.getFloat("DiscountAmount"));
                listDiscount.add(r);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listDiscount;
    }
     public Discount getDiscountByID(int DiscountID) {

        Discount dc = new Discount();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblDiscount where DiscountID = ? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, DiscountID);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                
                dc.setDiscountID(rs.getInt("DiscountID"));
                dc.setDiscountName(rs.getString("DiscountName"));
                dc.setDiscountPercent(rs.getFloat("DiscountPercent"));
                dc.setDiscountAmount(rs.getFloat("DiscountAmount"));
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return dc;
     }
    
    
}
